package com._520it.wms.util;

import com._520it.wms.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

//解耦操作
public class UserContext {

    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String PERMISSMION_IN_SESSION = "PERMISSMION_IN_SESSION";

    public static HttpSession getSession() {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return session;
    }

    //1往session中添加用户信息
    public static void setCurrentUser(Employee currentUser) {
            getSession().setAttribute(UserContext.USER_IN_SESSION, currentUser);

    }
    //2从session获取用户信息
    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(UserContext.USER_IN_SESSION);
    }

    //3往session中添加用户权限列表
    public static void setPermissionList(List<String> permissionList) {
        getSession().setAttribute(UserContext.PERMISSMION_IN_SESSION, permissionList);
    }

    //4从session中获取到用户的权限列表
    public static List<String> getPermissionList() {
        return (List<String>) getSession().getAttribute(UserContext.PERMISSMION_IN_SESSION);
    }
}
